﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace FubiManger.UI
{
    public class AesUtil
    {
        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="cipherText"></param>
        /// <returns></returns>
        public static string DecryptString(string cipherText)
        {
            // Instantiate a new Aes object to perform string symmetric encryption
            Aes encryptor = Aes.Create();

            encryptor.Mode = CipherMode.CBC;
            encryptor.Padding = PaddingMode.PKCS7;
            encryptor.KeySize = 128;


            byte[] key32 = new byte[32];
            //如果我们的密钥不是32为，则自动补全到32位
            byte[] byteKey = Encoding.UTF8.GetBytes("MerchantKeycodedMerchantKeycoded".PadRight(key32.Length));
            //复制密钥
            Array.Copy(byteKey, key32, key32.Length);

            //使用16位向量
            byte[] iv16 = new byte[16];
            //如果我们的向量不是16为，则自动补全到16位
            byte[] byteIv = Encoding.UTF8.GetBytes("160qwehydsde861f".PadRight(iv16.Length));
            //复制向量
            Array.Copy(byteIv, iv16, iv16.Length);
            encryptor.Key = key32;
            encryptor.IV = iv16;

            // Instantiate a new MemoryStream object to contain the encrypted bytes
            MemoryStream memoryStream = new MemoryStream();

            // Instantiate a new encryptor from our Aes object
            ICryptoTransform aesDecryptor = encryptor.CreateDecryptor();

            // Instantiate a new CryptoStream object to process the data and write it to the 
            // memory stream
            CryptoStream cryptoStream = new CryptoStream(memoryStream, aesDecryptor, CryptoStreamMode.Write);

            // Will contain decrypted plaintext
            string plainText = String.Empty;

            try
            {
                // Convert the ciphertext string into a byte array
                byte[] cipherBytes = Convert.FromBase64String(cipherText);

                // Decrypt the input ciphertext string
                cryptoStream.Write(cipherBytes, 0, cipherBytes.Length);

                // Complete the decryption process
                cryptoStream.FlushFinalBlock();

                // Convert the decrypted data from a MemoryStream to a byte array
                byte[] plainBytes = memoryStream.ToArray();

                // Convert the decrypted byte array to string
                plainText = Encoding.UTF8.GetString(plainBytes, 0, plainBytes.Length);
            }
            finally
            {
                // Close both the MemoryStream and the CryptoStream
                memoryStream.Close();
                cryptoStream.Close();
            }

            // Return the decrypted data as a string
            return plainText;
        }
        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="plainText"></param>
        /// <param name="key"></param>
        /// <param name="iv"></param>
        /// <returns></returns>
        public static string EncryptString(string plainText)
        {
            // Instantiate a new Aes object to perform string symmetric encryption
            Aes encryptor = Aes.Create();

            encryptor.Mode = CipherMode.CBC;
            encryptor.Padding = PaddingMode.PKCS7;
            encryptor.KeySize = 128;

            // Set key and IV
            byte[] key32 = new byte[32];
            //如果我们的密钥不是32为，则自动补全到32位
            byte[] byteKey = Encoding.UTF8.GetBytes("MerchantKeycodedMerchantKeycoded".PadRight(key32.Length));
            //复制密钥
            Array.Copy(byteKey, key32, key32.Length);

            //使用16位向量
            byte[] iv16 = new byte[16];
            //如果我们的向量不是16为，则自动补全到16位
            byte[] byteIv = Encoding.UTF8.GetBytes("160qwehydsde861f".PadRight(iv16.Length));
            //复制向量
            Array.Copy(byteIv, iv16, iv16.Length);
            encryptor.Key = key32;
            encryptor.IV = iv16;

            // Instantiate a new MemoryStream object to contain the encrypted bytes
            MemoryStream memoryStream = new MemoryStream();

            // Instantiate a new encryptor from our Aes object
            ICryptoTransform aesEncryptor = encryptor.CreateEncryptor();

            // Instantiate a new CryptoStream object to process the data and write it to the 
            // memory stream
            CryptoStream cryptoStream = new CryptoStream(memoryStream, aesEncryptor, CryptoStreamMode.Write);

            // Convert the plainText string into a byte array
            byte[] plainBytes = Encoding.UTF8.GetBytes(plainText);

            // Encrypt the input plaintext string
            cryptoStream.Write(plainBytes, 0, plainBytes.Length);

            // Complete the encryption process
            cryptoStream.FlushFinalBlock();

            // Convert the encrypted data from a MemoryStream to a byte array
            byte[] cipherBytes = memoryStream.ToArray();

            // Close both the MemoryStream and the CryptoStream
            memoryStream.Close();
            cryptoStream.Close();

            // Convert the encrypted byte array to a base64 encoded string
            string cipherText = Convert.ToBase64String(cipherBytes, 0, cipherBytes.Length);

            // Return the encrypted data as a string
            return cipherText;
        }
    }
}
